package me.wz.util.sdbc;

import java.util.*;

public class Native {
    public static interface INative {
        public String getExpression();
    }

    public static class NDate extends Date implements INative {
        private static final long serialVersionUID = 2249631655718212563L;
        private String expression;
        public NDate(String expression) { this.expression = expression; }
        public String getExpression() { return expression; }
        public void setExpression(String expression) { this.expression = expression; }
    }
    public static NDate Date(String str) { return new NDate(str); }
    public static final NDate SYSDATE = Date("sysdate()");

    public static String getExpression(Object primitive) {
        if( INative.class.isInstance(primitive) ) {
            return ((INative)primitive).getExpression();
        }
        if( mapp.get() != null ) {
            return mapp.get().get(primitive);
        }
        return null;
    }

    private static ThreadLocal<Map<Object, String>> mapp = TLUtil.NATIVE;

    private static <T>T map(T key, String expression) {
        if( mapp.get() == null ){
            mapp.set(new HashMap<Object, String>());
        }
        mapp.get().put(key, expression);
        return key;
    }

    public static Long Long(String expression) {
        return map(new Long(new Random().nextLong()), expression);
    }
    public static Integer Integer(String expression) {
        return map(new Integer(new Random().nextInt()), expression);
    }
    public static String String(String expression) {
        return map(new String(UUID.randomUUID().toString()), expression);
    }

    public static void clear(){ mapp.remove(); }
}
